﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using XCode;
using System.Diagnostics;
using NewLife.Configuration;
using NewLife.Reflection;
using System.Web;

namespace BingZi.HY.Core
{
    /// <summary>
    /// 模版格式化方案
    /// </summary>
    public class TemplateFormat
    {

        #region "模版基本属性"


        private BaseTemplatePage _Page = new BaseTemplatePage();
        /// <summary>
        /// 模版的页面
        /// </summary>
        public BaseTemplatePage Page
        {
            get { return _Page; }
            set { _Page = value; }
        }



 
        
        #endregion

        #region "==关于跳转链接=="

        /// <summary>
        /// 登录URL
        /// </summary>
        /// <returns></returns>
        public String GoLoginUrl()
        {
            return String.Format("/LoginAndReg.aspx?ReturnUrl={0}", HttpUtility.UrlEncode(Common.GetScriptUrl));
        }

        public String GoLoginOutUrl()
        {
            return String.Format("/LoginAndReg.aspx?Type=LoginOut&ReturnUrl={0}", HttpUtility.UrlEncode(Common.GetScriptUrl));
        }

        #endregion

        #region "==关于用户信息=="

        /// <summary>
        /// 获取登录用户编号
        /// </summary>
        public Int32 Get_UserID
        {
            get { return Page.Get_UserID; }
        }

        /// <summary>
        /// 获取登录用户名称
        /// </summary>
        public String Get_UserName
        {
            get { return Page.Get_UserName; }
        }

        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        public sys_UserTable Get_UserInfo
        {
            get { return Page.Get_UserInfo; }
        }

        /// <summary>
        /// 获取登录用户是否登录
        /// </summary>
        public Boolean Get_IsLogin
        {
            get { return Page.Get_IsLogin; }
        }


        #endregion

        #region "==关于站点SEO信息=="

        /// <summary>
        /// 页面标题
        /// </summary>
        public String Page_Title
        {
            get { return Page.Page_Title; }
        }

        /// <summary>
        /// 页面描述
        /// </summary>
        public String Page_MetaDescription
        {
            get { return Page.Page_MetaDescription; }
        }


        /// <summary>
        /// 页面关键字
        /// </summary>
        public String Page_MetaKeywords
        {
            get { return Page.Page_MetaKeywords; }
        }

        /// <summary>
        /// 页面关键字
        /// </summary>
        public String Page_Copyright
        {
            get { return Page.Page_Copyright; }
        }

        #endregion
        
        #region "==关于帮助信息调用成HTML模块=="

        /// <summary>
        /// 调用HTML模块
        /// </summary>
        /// <param name="HelpName">帮助名</param>
        /// <param name="ContainerName">容器名</param>
        /// <returns></returns>
        public String ViewHtmlContent(String HelpName, String ContainerName)
        {
            String Content = String.Empty;

            //读取容器内容
            String ContainerPath = Page.Server.MapPath(String.Format("{0}{1}", Page.TemplatePath, ContainerName));
            using (StreamReader sr = File.OpenText(ContainerPath))
            {
                Content = sr.ReadToEnd();
                if (!String.IsNullOrEmpty(Content))
                {
                    Content = Content.Replace("[Title]", HelpName);

                    //调用Help内容
                    app_Help help = app_Help.Find(app_Help._.H_Name, HelpName);
                    if (help != null && help.HelpID > 0)
                    {
                        Content = Content.Replace("[Content]", help.H_Description);
                    }
                    else
                    {
                        Content = Content.Replace("[Content]", HelpName + "还未增加内容");
                    }
                }
            }
            return Content;
        }

        /// <summary>
        /// 调用HTML模块
        /// </summary>
        /// <param name="HelpName">帮助名</param>
        /// <param name="ContainerName">容器名</param>
        /// <returns></returns>
        public String ViewHtmlContent(String HelpName)
        {
            String Content = String.Empty;
 
            //调用Help内容
            app_Help help = app_Help.Find(app_Help._.H_Name, HelpName);
            if (help != null && help.HelpID > 0)
            {
                Content = help.H_Description;
            }
            else
            {
                Content =HelpName+ "还未增加内容";
            }
            return Content;
        }


        #endregion

        #region "==关于分类模块=="
 
        public  List<app_Category> ViewFindChild(object ParentID)
        {
            return app_Category.ViewFindChild(ParentID);
        }
        #endregion

        #region "==关于商品模块=="
        /// <summary>
        /// 获取分类下商品的数量
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public List<app_Product> GetProductListByCategory(app_Category Category)
        {
            return app_Product.GetProductListByCategory(Category.CategoryID);
        }
        #endregion

        #region "==关于首页内容模块调用=="
        /// <summary>
        /// 折扣价格
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public double ViewPriceDiscount(app_Product p)
        {
            return (p.PriceOriginal - p.PriceNow);
        }   
        /// <summary>
        /// 打了多少折扣
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public String ViewProductDiscount(app_Product p)
        {
            return ((p.PriceNow / p.PriceOriginal) * 10).ToString("f1");
        }
        /// <summary>
        /// 获得置顶商品
        /// </summary>
        /// <returns></returns>
        public app_Product GetTopProduct()
        {
            app_Product p=app_Product.GetTopProduct();
            return p;
        }
        /// <summary>
        /// 所有没有过期的团购
        /// </summary>
        /// <returns></returns>
        public List<app_Product> GetNowProduct()
        {
            return app_Product.GetNowProduct();
        }


        /// <summary>
        /// 倒计时
        /// </summary>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public TimeSpan ViewDiffDate(app_Product Product)
        {
            return ViewDiffDate(Product.EndTime);
        }

        /// <summary>
        /// 倒计时
        /// </summary>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public TimeSpan ViewDiffDate(DateTime EndTime)
        {
            return EndTime - DateTime.Now;
        }
        /// <summary>
        /// 显示商品图片
        /// </summary>
        /// <param name="Product">商品</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public String ViewPicture(app_Product Product,Boolean IsMain)
        {
            String PictureUrl = String.Format("{0}images/No_picture.gif", Page.TemplatePath);
            if (!String.IsNullOrEmpty(Product.Picture))
            {
                PictureUrl = String.Format(Product.Picture, IsMain ? "" : "s_");
            }
            return PictureUrl;
        }


        /// <summary>
        /// 多少人购买成功
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int GetCompleteOrder(app_Product p)
        {
            return app_Order.GetCompleteOrder(p.ID);
        }
 
        #endregion

        #region "==关于帮助内容模块调用=="
        /// <summary>
        /// 根据上级编号获取帮助列表
        /// </summary>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public EntityList<app_Help> FindHelpChildByParentID(Int32 ParentID)
        {
            return app_Help.FindChildByParentID(ParentID);
        }



        #endregion

        #region "==关于消息页面代码=="


        /// <summary>
        /// 根据icon类型返回图片名称
        /// </summary>
        /// <param name="I_type"></param>
        /// <returns></returns>
        public string Get_M_IconType(Icon_Type I_type)
        {
            switch (I_type)
            {
                case Icon_Type.Error:
                    return "MessageError.gif";
                case Icon_Type.Alert:
                    return "MessageAlert.gif";
                case Icon_Type.OK:
                    return "MessageOk.gif";
            }
            return "MessageAlert.gif";
        }

        public string Get_M_ButtonList(List<sys_NavigationUrl> M_ButtonList)
        {
            string DefaultSelectButtonName = "";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < M_ButtonList.Count; i++)
            {
                string NameKey = string.Format("Message_Button{0}", i);
                string url = "";
                if (M_ButtonList[i].btnDefaultSelect)
                    DefaultSelectButtonName = NameKey;
                switch (M_ButtonList[i].btnType)
                {
                    case UrlType.Href:
                        url = string.Format("JavaScript:window.location.href='{0}';",Page. ResolveClientUrl(M_ButtonList[i].btnUrl));
                        break;
                    case UrlType.JavaScript:
                        url = string.Format("JavaScript:{0}", M_ButtonList[i].btnUrl);
                        break;
                    case UrlType.VBScript:
                        url = string.Format("VBScript:{0}", M_ButtonList[i].btnUrl);
                        break;
                }
                sb.AppendFormat("<input type='button' ID='{3}' class='button_bak' onClick=\"{0}\" value='{1}' title='{2}'> ", url, M_ButtonList[i].btnText, M_ButtonList[i].btnHint, NameKey);
            }

            if (DefaultSelectButtonName != "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "kk", string.Format("document.getElementById('{0}').focus();", DefaultSelectButtonName), true);
            }
            return sb.ToString();
        }
        #endregion

        #region "模版容器构造"
        /// <summary>
        /// 显示子控件
        /// </summary>
        /// <param name="Title">标题</param>
        /// <param name="ContainerName">容器名</param>
        /// <param name="TemplateFile">子控件</param>
        /// <returns></returns>
        public String ViewControls(String Title, String ContainerName, String TemplateFile)
        {
            String Content = String.Empty;

            //读取容器内容
            String ContainerPath = Page.Server.MapPath(String.Format("{0}{1}",Page.TemplatePath,ContainerName));
           using(  StreamReader sr = File.OpenText(ContainerPath))
           {
                Content = sr.ReadToEnd();
                if(!String.IsNullOrEmpty(Content))
                {
                    Content = Content.Replace("[Title]", Title);
                    Content = Content.Replace("[Content]", Page.ViewTemplate(TemplateFile, Page.Template_Puts, this));
                }
           }
           return Content;
        }


        #endregion

        #region "模版格式化方案构造"

        /// <summary>
        /// 模版格式化方案构造
        /// </summary>
        /// <param name="__Page">模版基类的页面</param>
        public TemplateFormat(BaseTemplatePage __Page)
        {
            _Page = __Page;
        }


        #endregion
    }
}
